library(ggplot2)
x <- seq(0,1,0.01)
dat <- data.frame(x = x, y = 100*exp(12*(-0.5+x))/(1+exp(12*(-0.5+x))))

ggplot(dat, aes(x = x, y=y)) + 
  geom_line(size=1.2) +
  geom_hline(yintercept = 0) +
  labs(x="\u03b8", y="P(\u03b8)") +
  theme_classic() +
  theme(axis.title.x = element_text(size=28, hjust = 1, vjust=2),
        axis.title.y = element_text(size=28, angle = 0),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.line.x = element_blank(),
        plot.margin = margin(50,80,50,80))
ggsave("Figure1.png", width = 9, height = 6, units = "in", dpi = 350)

# intersection curves
x <- seq(0,1,0.01)
dat <- data.frame(x = x, y = 80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x))), z= -25+80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x)))+50*x)

ggplot(dat, aes(x = x)) + 
  geom_line(aes(y=y, colour="Y1"),size=1.5) +
  geom_line(aes(y=z, colour="Y2"),size=1.5) +
  geom_hline(yintercept = 0) +
  geom_text(aes(x = 0.5, y = -5, label = "M"), size=7) +
  geom_segment(aes(x = x, xend = x, y = 0, yend = y),
               data = dat[ dat$x %in% c(0.5), ],
               lty = "dashed",
               size=1.2) +
  scale_color_manual(values = c("Y1"="darkred", "Y2"="darkgreen"), # Color specification
                     labels = c("Free-riding", "Participating")) +
  labs(x="Proportion of others participating (theta-)", y="Individual i's utility") +
  theme_classic() +
  theme(legend.title = element_blank(),
        legend.text = element_text(size=26),
        axis.title = element_text(size=22),
        axis.text = element_text(size=20))
ggsave("Figure2a.png", width = 12, height = 6, units = "in", dpi = 350)

x <- seq(0,1,0.01)
dat <- data.frame(x = x, y = 80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x))), z= -25+80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x)))+50*x, w= -25+120*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x)))+50*x)

ggplot(dat, aes(x = x)) + 
  geom_line(aes(y=y, colour="Y1"),size=1.5) +
  geom_line(aes(y=z, colour="Y2"),size=1.5) +
  geom_line(aes(y=w, colour="Y3"),size=1.5) +
  geom_hline(yintercept = 0) +
  geom_text(aes(x = 0.4, y = -5, label = "M*"), size=7) +
  geom_text(aes(x = 0.5, y = -5, label = "M"), size=7) +
  geom_segment(aes(x = x, xend = x, y = 0, yend = y),
               data = dat[ dat$x %in% c(0.4, 0.5), ],
               lty = "dashed",
               size=1.2) +
  scale_color_manual(values = c("Y1"="darkred", "Y2"="darkgreen", "Y3"="darkblue"), # Color specification
                     labels = c("Free-riding", "Participating: Low V", "Participating: High V")) +
  labs(x="Proportion of others participating (theta-)", y="Individual i's utility") +
  theme_classic() +
  theme(legend.title = element_blank(),
        legend.text = element_text(size=26),
        axis.title = element_text(size=22),
        axis.text = element_text(size=20))
ggsave("Figure2b.png", width = 12, height = 6, units = "in", dpi = 350)

x <- seq(0,1,0.01)
dat <- data.frame(x = x, y = 80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x))), z= -25+80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x)))+50*x, w= -25+80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x)))+65*x)

ggplot(dat, aes(x = x)) + 
  geom_line(aes(y=y, colour="Y1"),size=1.5) +
  geom_line(aes(y=z, colour="Y2"),size=1.5) +
  geom_line(aes(y=w, colour="Y3"),size=1.5) +
  geom_hline(yintercept = 0) +
  geom_text(aes(x = 0.39, y = -5, label = "M*"), size=7) +
  geom_text(aes(x = 0.5, y = -5, label = "M"), size=7) +
  geom_segment(aes(x = x, xend = x, y = 0, yend = y),
               data = dat[ dat$x %in% c(0.39, 0.5), ],
               lty = "dashed",
               size=1.2) +
  scale_color_manual(values = c("Y1"="darkred", "Y2"="darkgreen", "Y3"="darkblue"), # Color specification
                     labels = c("Free-riding", "Participating: Low S", "Participating: High S")) +
  labs(x="Proportion of others participating (theta-)", y="Individual i's utility") +
  theme_classic() +
  theme(legend.title = element_blank(),
        legend.text = element_text(size=26),
        axis.title = element_text(size=22),
        axis.text = element_text(size=20))
ggsave("Figure2c.png", width = 12, height = 6, units = "in", dpi = 350)

x <- seq(0,1,0.01)
dat <- data.frame(x = x, y = 80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x))), z= -20+80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x)))+50*x, w= -25+80*exp(10*(-0.6+x))/(1+exp(10*(-0.6+x)))+50*x)

ggplot(dat, aes(x = x)) + 
  geom_line(aes(y=y, colour="Y1"),size=1.5) +
  geom_line(aes(y=z, colour="Y2"),size=1.5) +
  geom_line(aes(y=w, colour="Y3"),size=1.5) +
  geom_hline(yintercept = 0) +
  geom_text(aes(x = 0.4, y = -5, label = "M"), size=7) +
  geom_text(aes(x = 0.5, y = -5, label = "M*"), size=7) +
  geom_segment(aes(x = x, xend = x, y = 0, yend = y),
               data = dat[ dat$x %in% c(0.4, 0.5), ],
               lty = "dashed",
               size=1.2) +
  scale_color_manual(values = c("Y1"="darkred", "Y2"="darkgreen", "Y3"="darkblue"), # Color specification
                     labels = c("Free-riding", "Participating: Low c", "Participating: High c")) +
  labs(x="Proportion of others participating (theta-)", y="Individual i's utility") +
  theme_classic() +
  theme(legend.title = element_blank(),
        legend.text = element_text(size=26),
        axis.title = element_text(size=22),
        axis.text = element_text(size=20))
ggsave("Figure2d.png", width = 12, height = 6, units = "in", dpi = 350)
